package charts; 
import charts.series.Element;
import charts.series.bars.StackCollection;
import charts.Structs.StyleStruct;
import string.Utils;
import hxjson2.JSON;
import flash.geom.Point;


class BarStack extends BarBase<StyleStruct, StackCollection> {

	public function new( json:Dynamic, num:Float, group:Float ) {
		
		super(null, 0);
		
		this.style = {
			colours:			['#FF0000','#00FF00'],	// <-- ugly default colours
			values:				[],
			keys:				[],
			tip:				'#x_label# : #val#<br>Total: #total#',
			alpha:				0.6
		};
		
		Object_helper.merge_2( json, style );
		
		//this.axis = which_axis_am_i_attached_to(data, num);
		
		//
		// bars are grouped, so 3 bar sets on one chart
		// will arrange them selves next to each other
		// at each value of X, this.group tell the bar
		// where it is in that grouping
		//
		this.group = group;
		
		this.values = this.style.values;

		this.add_values();
	}
	
	//
	// return an array of key info objects:
	//
	public override function get_keys(): BaseKeyType {
		
		var arr = new Array<BaseKeyType>(); // TODO + figure out the type => Same as Base.hx

		for ( o in this.style.keys ) {
			if ( o.text && o.font_size && o.colour ) {
				o.colour = string.Utils.get_colour( o.colour );
				arr.push( o );
			}
		}
		
		return arr;
	}
	
	//
	// value is an array (a stack) of bar stacks
	//
	override function get_element( index:Float, value:Array<BarStack> ): StackCollection {
		
		//
		// this is the style for a stack:
		//
		var default_style:Style = {
			tip		: this.style.tip
			, values	: value
			, colours	: this.style.colours
			, alpha		: this.style.alpha
		};
		
		return new StackCollection( index, default_style, this.group );
	}
	
	
	//
	// get all the elements at this X position
	//
	override function get_all_at_this_x_pos( x:Float ) : Array<StackCollection> {
		
		var arr = new Array<StackCollection>();
		var p:flash.geom.Point;
		var e:StackCollection;
		
		for ( i in 0...this.numChildren) {
		
			// some of the children will will mask
			// Sprites, so filter those out:
			//
			if( Std.is( this.getChildAt(i), Element) ) {
	
				e = cast( this.getChildAt(i), StackCollection);
			
				p = e.get_mid_Point();
				if ( p.x == x ) {
					var children:Array<Dynamic> = e.get_children();
					for (child in children )
						arr.push( child );
				}
			}
		}
		
		return arr;
	}
}
